﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using BaseUI;
using BL;
using IUI;

namespace WinUI
{
   public partial class MainForm : //Form
                                   BaseWinForm<MainPageController>, IMainPage
   {
       static private MainForm _instance = null;
       static public MainForm Instance
       {
           get
           {
               if (null == _instance)
               {
                   _instance = new MainForm();
               }

               return _instance;
           }
       }
      
      private MainForm()
      {
          InitializeComponent();
      }

      #region Реализация IMainPage
      private String _userID;

      public User User
      {
          get { return User.GetByName(_userID); }
      }

      public void SetUserID(String id)
      {
          if (null == _userID)
          {              
              _userID = id;
          }
      }

      public void OpenAdminPage()
      {
          if (User.IsAdmin)
          {
              this.Hide();

              AdminForm af = new AdminForm();
              af.ShowDialog();
          }
      }

      void IMainPage.AddItem(IPlayFactory factory)
      {
          for (int i = 0; i < lst_Choise.Items.Count; i++ )
          {
              if (lst_Choise.GetSelected(i))
              {
                  Guid g = new Guid((String)_listOfChoise.Rows[i]["id"]);
                  IPlayable p = factory.GetItem(g);

                  lst_Current.Items.Add(p);
              }
          }
      }

       void IMainPage.DelItem()
       {
           IPlayable p = null;
           for (int i = 0; i < lst_Current.Items.Count; i++)
           {
               if (lst_Current.GetSelected(i))
               {
                   p = (IPlayable) lst_Current.Items[i];
                   break;
               }
           }

           if (null != p)
           {
               lst_Current.Items.Remove(p);
           }
       }

       void IMainPage.EditItem()
       {
           IPlayable p = null;
           for (int i = 0; i < lst_Current.Items.Count; i++)
           {
               if (lst_Current.GetSelected(i))
               {
                   p = (IPlayable)lst_Current.Items[i];
                   break;
               }
           }

           if (null != p)
           {
               if (p is Playlist)
               {
                   ManagePlaylistForm mpf = new ManagePlaylistForm((Playlist) p);
                   mpf.ShowDialog();
               }
           }
       }


      DataTable _listOfChoise;
      String IMainPage.CurrentChoise
      {
          get
          {
              return _listOfChoise.TableName;
          }
      }

      public void Fill_ListOfChoise(DataTable tbl)
      {
          _listOfChoise = tbl;

          lst_Choise.Items.Clear();
          foreach (var row in _listOfChoise.Rows)
          {
              String item = "";
              foreach (var column in _listOfChoise.Columns)
              {
                  String column_name = ((DataColumn)column).ColumnName;
                  if ("id" != column_name)
                  {
                      item += (String)((DataRow)row)[column_name] + " - ";
                  }
              }

              item = item.Remove(item.Length - 3);
              lst_Choise.Items.Add(item);
          }
      }

       // обработчик события нажатия на кнопку "Admin Page"
      private event EventHandler _btnAdminClick;
      EventHandler IMainPage.Click_AdminButton
      {
          get { return _btnAdminClick; }
          set { _btnAdminClick = value; }
      }

      private EventHandler _click_Playlists;
      EventHandler IMainPage.Click_Playlists
      {
          get
          {
              return _click_Playlists;
          }
          set
          {
              _click_Playlists = value;
          }
      }

      private EventHandler _click_Songs;
      EventHandler IMainPage.Click_Songs
      {
          get
          {
              return _click_Songs;
          }
          set
          {
              _click_Songs = value;
          }
      }

      private EventHandler _click_AddItem;
      EventHandler IMainPage.Click_AddItem
      {
          get
          {
              return _click_AddItem;
          }
          set
          {
              _click_AddItem = value;
          }
      }

      private EventHandler _click_DeleteItem;
      EventHandler IMainPage.Click_DeleteItem
      {
          get
          {
              return _click_DeleteItem;
          }
          set
          {
              _click_DeleteItem = value;
          }
      }

      private EventHandler _click_EditItem;
      EventHandler IMainPage.Click_EditItem
      {
          get
          {
              return _click_EditItem;
          }
          set
          {
              _click_EditItem = value;
          }
      }


      private EventHandler _click_NewSong;
      EventHandler IMainPage.Click_NewSong
      {
          get
          {
              return _click_NewSong;
          }
          set
          {
              _click_NewSong = value;
          }
      }
      #endregion
       
      private void MainForm_Load(object sender, EventArgs e)
      {
          this.Hide();
          LoginForm lf = new LoginForm();
          lf.ShowDialog();
      }

      private void MainForm_Shown(object sender, EventArgs e)
      {
          if (User.IsAdmin)
          {
              btnAdmin.Enabled = true;
              btnAdmin.Visible = true;
          } 
      }

      private void btnAdmin_Click(object sender, EventArgs e)
      {
          if (_btnAdminClick != null)
              _btnAdminClick(this, null);
      }

      private void btn_Playlists_Click(object sender, EventArgs e)
      {
          if (_click_Playlists != null)
              _click_Playlists(this, null);
      }

      private void btn_AddItem_Click(object sender, EventArgs e)
      {
          if (_click_AddItem != null)
              _click_AddItem(this, null);
      }

      private void btn_Songs_Click(object sender, EventArgs e)
      {
          if (_click_Songs != null)
              _click_Songs(this, null);
      }

      private void btn_Delete_Click(object sender, EventArgs e)
      {
          if (_click_DeleteItem != null)
              _click_DeleteItem(this, null);
      }

      private void btn_Edit_Click(object sender, EventArgs e)
      {
          if (_click_EditItem != null)
              _click_EditItem(this, null);
      }

      private void btn_NewSong_Click(object sender, EventArgs e)
      {
          if (_click_NewSong != null)
              _click_NewSong(this, null);
      }
   }
}
